package com.zys.el.user.util.html;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.zys.el.user.util.baidu.translate.demo.TransApi;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

@Component
public class acm {
    private static final String APP_ID = "20230414001641995";
    private static final String SECURITY_KEY = "Ch9hDhMBVE29c7wcTs6h";

    public  Map<String, Date> getNew() {
        String url = "https://www.acm.org/news";
        Map<String, Date> re = new HashMap<>();
        try {
            Document doc = Jsoup.connect(url).get();
//            System.out.println(doc);
            Elements newsList = doc.select("div.list-component__list-item--inline");

            for (Element news : newsList) {
//                System.out.println(news.select("time").text());
//                System.out.println(news.select("h5.item-component__item-level").text());

                String tanslator = acm.tanslator(news.select("h5.item-component__item-level").text());
                Date time = acm.StringToDate(news.select("time").text());
                re.put(tanslator, time);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return re;
    }

    private static String tanslator(String src) throws UnsupportedEncodingException {
        TransApi api = new TransApi(APP_ID, SECURITY_KEY);
        String query = src;
        String transResult = api.getTransResult(query, "en", "zh");
        String utf8Str = new String(transResult.getBytes("UTF-8"), "UTF-8");

        JSONObject jsonObject = JSONObject.parseObject(utf8Str);
        JSONArray transResultArray = jsonObject.getJSONArray("trans_result");
        String dst = transResultArray.getJSONObject(0).getString("dst");

        return dst;
    }

    private static Date StringToDate(String src) throws ParseException {
        String dateString = src;
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("d MMMM yyyy", Locale.ENGLISH);
        LocalDate date = LocalDate.parse(dateString, inputFormatter);

        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yy-MM-dd HH:mm:ss");
        String formattedDate = date.atStartOfDay().format(outputFormatter);

        SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        Date re = formatter.parse(formattedDate);
//        System.out.println(re);
        return re;


    }
}

